package hdapi3;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class Cache {
	private final static int MAX_ENTRIES = 30000;
	private Map<String, Object> map = Collections.synchronizedMap(new LinkedHashMap<String, Object>(MAX_ENTRIES + 1, 0.75F, true) {
		@Override
		protected boolean removeEldestEntry(Map.Entry<String, Object> eldest) {
	        return size() > MAX_ENTRIES;
	    }
	});
	
	public void put(String key, Object value) {
		map.put(key, value);
	}
	
	public Object get(String key) {
		return map.get(key);
	}
	
	public static void main(String[] args) throws Exception{
		final Cache myCache = new Cache();
		Thread t1 = new Thread() {
			@Override
			public void run() {
				myCache.put("k1", "v1");
				myCache.put("k2", "v2");
				myCache.put("k3", "v3");
			}
		};
		Thread t2 = new Thread() {
			@Override
			public void run() {
				myCache.put("k4", "v4");
				myCache.put("k5", "v5");
				myCache.put("k6", "v6");
			}
		};
		Thread t3 = new Thread() {
			@Override
			public void run() {
				myCache.put("k7", "v7");
				myCache.put("k8", "v8");
				myCache.put("k9", "v9");
			}
		};
		t3.start();
		t2.start();
		t1.start();
		while (t1.isAlive() || t2.isAlive() || t3.isAlive()) {
			Thread.sleep(3000);
		}
		System.out.println(myCache.get("k1"));
		System.out.println(myCache.get("k2"));
		System.out.println(myCache.get("k3"));
		System.out.println(myCache.get("k4"));
		System.out.println(myCache.get("k5"));
		System.out.println(myCache.get("k6"));
		System.out.println(myCache.get("k7"));
		System.out.println(myCache.get("k8"));
		System.out.println(myCache.get("k9"));
		
	}
}
